package com.adnaan.backend.controller.admin;

import com.adnaan.backend.common.annotation.RestControllerMapping;
import com.adnaan.backend.common.model.PageParams;
import com.adnaan.backend.common.model.Result;
import com.adnaan.backend.entity.ArticleTags;
import com.adnaan.backend.service.ArticleTagsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * author: adnaan
 * create: 2025-02-19 13:13
 * Description:
 */
@Tag(name = "文章标签管理")
@RestControllerMapping("/articleTags")
public class ArticleTagsController {

    @Resource
    private ArticleTagsService articleTagsService;

    @PostMapping("/page")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "文章标签分页查询")
    public Result<Page<ArticleTags>> pageQuery(@RequestBody PageParams<ArticleTags> pageParams) {
        // 1.查询参数
        ArticleTags params = pageParams.getParams();
        LambdaQueryWrapper<ArticleTags> lqw = new LambdaQueryWrapper<>();
        // 2.查询条件
        lqw.like(params.getTagName() != null, ArticleTags::getTagName, params.getTagName());
        // 3.分页查询
        Page<ArticleTags> page = articleTagsService.page(pageParams.getPage(), lqw);
        return Result.success(page);
    }

    @PostMapping("/insert")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "新增文章标签数据")
    public Result<Boolean> insert(@RequestBody ArticleTags articleTags) {
        if (articleTagsService.save(articleTags)) {
            return Result.success();
        }
        return Result.error();
    }

    @PostMapping("/update")
    @ApiOperationSupport(order = 3)
    @Operation(summary = "更新文章标签数据")
    public Result<Boolean> update(@RequestBody ArticleTags articleTags) {
        if (articleTagsService.updateById(articleTags)) {
            return Result.success();
        }
        return Result.error();
    }

    @GetMapping("/delete/{id}")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "通过主键删除文章标签数据")
    public Result<Boolean> deleteById(@PathVariable String id) {
        if (articleTagsService.removeById(id)) {

            return Result.success();
        }
        return Result.error();
    }
}
